package com.pipodi.itasamobile.connections;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.client.ClientProtocolException;

import com.pipodi.itasamobile.MainActivity;
import com.pipodi.itasamobile.utils.LoginVariables;

import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

public class ConnectionForSubtitle extends AsyncTask<String, String, String>{
	
	private int id;
	
	public ConnectionForSubtitle(int id){
		this.id = id;
	}
	
	@Override
	protected String doInBackground(String... params) {
		POSTCallLoginItaSA logInCall = new POSTCallLoginItaSA(10000);
		try {
			logInCall.connectItasa("Pipodi", "140507");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String servicestring = Context.DOWNLOAD_SERVICE;
	    DownloadManager downloadmanager;
	    downloadmanager = (DownloadManager) MainActivity.context.getSystemService(servicestring);
	    Uri uri = Uri
	      .parse("https://api.italiansubs.net/api/rest/subtitles/download?subtitle_id=" + this.id + "&authcode=" + "0a7623231022fde8b519d5f6d3084700" + "&apikey=" + Constants.APIKey);
	    Log.e("URL", "https://api.italiansubs.net/api/rest/subtitles/download?subtitle_id=" + this.id + "&authcode=" + "0a7623231022fde8b519d5f6d3084700" + "&apikey=" + Constants.APIKey);
	    DownloadManager.Request request = new Request(uri);
	    request.setAllowedNetworkTypes(
                DownloadManager.Request.NETWORK_WIFI
                        | DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(false);
	    Long reference = downloadmanager.enqueue(request);
		return null;
	}
	
}
